package com.lcxw.hxzbapi.entity.response;

import com.lcxw.hxzbcommon.entity.ErrorCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

@Setter
@Getter
@ToString
@Component
public class Response {

    protected int code;
    protected String desc;
    protected Object data;

    public static Response wrongParam(Logger logger,String paramName){
        logger.warn("invalid params:{}",paramName);
        Response response = new Response();
        response.setCode(ErrorCode.PARAM_ERROR);
        response.setDesc("parameter:{"+paramName+"} must not be null,or invalid format.");
        return response;
    }

    public static Response dbFail(Logger logger,String operation){
        logger.warn("{} failed ,db operate failed",operation);
        Response response = new Response();
        response.setCode(ErrorCode.INTERNAL_ERROR);
        response.setDesc("数据库操作失败，请稍后再试。");
        return response;
    }

    public static Response common(Logger logger,String logInfo,int code,String desc,Object data){
        logger.warn(logInfo);
        Response response = new Response();
        response.setCode(code);
        response.setDesc(desc);
        response.setData(data);
        return response;
    }

    public static Response ok(){
        Response response = new Response();
        response.setCode(ErrorCode.SUCCESS);
        response.setDesc("success");
        response.setData(null);
        return response;
    }

    public static Response ok(Object data){
        Response response = new Response();
        response.setCode(ErrorCode.SUCCESS);
        response.setDesc("success");
        response.setData(data);
        return response;
    }

    public static Response error(int code,String desc){
        Response response = new Response();
        response.setCode(code);
        response.setDesc(desc);
        return response;
    }

    public static Response error(int code,String desc,Object data){
        Response response = new Response();
        response.setCode(code);
        response.setDesc(desc);
        response.setData(data);
        return response;
    }

}
